<?php 
use yii\helpers\Html;
use yii\helpers\Url;
use yii\bootstrap\ActiveForm;
use yii\helpers\ArrayHelper;
use common\models\User;

$this->title = 'add';
?>
	<style>
		.info-title{text-align:center;}
		.search_box{display:none;}
		.search_box .zhezhao{position:fixed;top:0;left:0;right:0;bottom:0;background:#000;opacity:0.6;}
		.search_box .search_form{position:absolute;top:10%;width:60%;background:#FFF;left:20%;}
		.search_box .search_form .form-group{margin-right: 0;margin-left:0;}
		.searchsex_box{overflow:hidden;}
		.searchsex_box>span{float:left;margin-top:10px;font-weight:700;}
		.searchsex_box .radio{float:left;margin-left:50px;}
		.search_form .box-footer{text-align:center;}
		.search_form .box-footer button{width:200px;}
		.font-info{margin-left:450px;}
		.trans_user span:hover{text-decoration:underline;}
	</style>

    <section class="content">
      <div class="row">
      	<div class="col-md-12">
          <div class="box box-danger">
            <div class="box-header with-border">
              <h3 class="box-title">新建订单</h3>
            </div>
            <div class="box-body">
	            <div class="info-title">
	              <h4>部门承接信息</h4>
	            </div>
	            <?php $form = ActiveForm::begin(['action' => ['order/add'], 'method' => 'post']);?>
				    <div class="form-group">	
		              <div class="row">
						<div class="col-xs-3">
						  <label>选择流程</label>
						  <?=Html::dropDownList('pcode', $model->pcode, $process, ['class'=>'form-control process', 'readonly' => true, 'prompt' => '选择流程']);?>
						  <?=Html::error($model, 'pcode', ['style' => 'color:red'])?>
						</div>
						<div class="col-xs-3">
							<label>承接关系</label>
								<select class="form-control transfer_undertake" name="pid"> 
										
								</select>
						</div>
						<div class="col-xs-3">
							<label>订单所属城市</label>
							<select class="form-control" name="order_home"> 
								<option value="太原市">太原市</option>
								<option value="阳泉市">阳泉市</option>
								<option value="大同市">大同市</option>
								<option value="临汾市">临汾市</option>
								<option value="长治市">长治市</option>
								<option value="晋城市">晋城市</option>
								<option value="晋中市">晋中市</option>
								<option value="吕梁市">吕梁市</option>
								<option value="忻州市">忻州市</option>
								<option value="运城市">运城市</option>
								<option value="朔州市">朔州市</option>	
							</select>
						</div>
					 </div>
					</div>
					<div class="form-group">
		              <div class="row">
		                <div class="col-xs-3">
		                  <label>转接人</label>
		                  <input type="text" class="form-control" value="<?= (User::findOne(Yii::$app->user->id)->employee)?User::findOne(Yii::$app->user->id)->employee->realname:''; ?> " readonly/>
		                  <input type="hidden" name="transfer" value="<?= (User::findOne(Yii::$app->user->id)->employee)?User::findOne(Yii::$app->user->id)->employee->id:'';?>">
		                  <?=Html::error($model, 'transfer', ['style' => 'color:red'])?>
		                </div>
		                <div class="col-xs-3">
		                  <label>承接人</label>
		                  <input type="text" class="form-control undertake" readonly>
		                  <input type="hidden" name="undertake" class="underval">
		                  <?=Html::error($model, 'undertake', ['style' => 'color:red'])?>
		                </div>
		                <div class="col-xs-3">
		                  <label>对接人</label>
		                  <input type="text" class="form-control username" />
		                  <input type="hidden" name="user" class="userval" />
		                  <?=Html::error($model, 'user', ['style' => 'color:red'])?>
		                </div>
		                <div class="col-xs-3">
		                  <label>填表日期</label>
		                  <input type="text" class="form-control datepicker" value="<?= date('Y-m-d', time())?>" name="init_form" readonly>
		                  <?=Html::error($model, 'init_form', ['style' => 'color:red'])?>
		                </div>
		              </div>
		            </div>
		            <div class="info-title">
		              <h4>客户信息</h4>
		            </div>
					<div class="form-group">
		              <div class="row">
		                <div class="col-xs-4">
		                  <label>客户</label>
		                  <?=Html::dropDownList('customer', '', $customer, ['class'=>'form-control customer', 'readonly' => true, 'prompt' => '选择客户']);?>
		                  <?=Html::error($model, 'customer', ['style' => 'color:red'])?>
		                </div>
		                <div class="col-xs-4">
		                  <label>性别</label>
		                  <input type="text" class="form-control customer-sex" readonly>
		                </div>
		                <div class="col-xs-4">
		                  <label>出生年月</label>
		                  <input type="text" class="form-control customer-birthday" readonly />
		                </div>
		              </div>
		            </div>
					<div class="form-group">
		              <div class="row">
		                <div class="col-xs-3">
		                  <label>检测项目</label>
		                  <?=Html::dropDownList('product', '', $product, ['class'=>'form-control choise_product', 'readonly' => true, 'prompt' => '选择产品']);?>
		                  <?=Html::error($model, 'product', ['style' => 'color:red'])?>
		                </div>
		                <div class="col-xs-3">
		                  <label>购买日期</label>
		                  <input type="text" class="form-control datepicker" name="purchase_time" value="<?= date('Y-m-d', time())?>" readonly>
		                  <?=Html::error($model, 'purchase_time', ['style' => 'color:red'])?>
		                </div>
		                <div class="col-xs-3">
		                  <label>检测时间</label>
		                  <input type="text" class="form-control datepicker" name="check_time" value="<?= date('Y-m-d', time())?>" readonly>
		                  <?=Html::error($model, 'check_time', ['style' => 'color:red'])?>
		                </div>
	                	<div class="col-xs-3">
		                  <label>产品价格</label>
		                  <input type="text" class="form-control product_price" name="price" readonly />
		                  <?=Html::error($model, 'price', ['style' => 'color:red'])?>
		                </div>
		              </div>
		            </div>
					<div class="form-group">
		              <div class="row">
		                <div class="col-xs-12">
		                  <label>检测地点</label>
		                  <textarea class="form-control" name="method"></textarea>
		                  <?=Html::error($model, 'method', ['style' => 'color:red'])?>
		                </div>
		              </div>
		            </div>
					<div class="form-group">
		              <div class="row">
		                <div class="col-xs-6">
		                  <label>产品缴费人</label>
		                  <input type="text" class="form-control" name="purchase_user">
		                  <?=Html::error($model, 'purchase_user', ['style' => 'color:red'])?>
		                </div>
		                <div class="col-xs-6">
		                  <label>缴费人与产品使用人关系</label>
		                  <input type="text" class="form-control" name="relation">
		                  <?=Html::error($model, 'relation', ['style' => 'color:red'])?>
		                </div>
		              </div>
		            </div>
	            </div>
            	<div class="box-footer">
				  <button class="btn btn-primary">确定</button>
				  <button class="btn btn-warning">取消</button>
				</div>
	         <?php ActiveForm::end();?>	   
            <!-- /.box-body -->
          </div>
      	</div>
      </div>
    </section>

	<div class="search_box">
	  <div class="zhezhao"></div>
	  <div class="search_form">
	      <div class="box-body">
	        <div class="form-group">
	          <label for="search_barcode">接待人分配</label>
	          <select class="form-control depart">
	            <option value=''>选择部门</option>
	          </select>
	          <select class="form-control employees" name="trans_user">
	            <option value="">选择接待人</option>
	          </select>
	          <span class="error-trans" style="color:red"></span>
	          <input type="hidden" name="intention" class="hide_intention" value="" />
	        </div>
	        </div>
	      <div class="box-footer">
	      <input class='btn btn-primary btn-submit' value="确定" readonly style="width:200px"/>
	      <input class="btn btn-warning btn-close" readonly value="关闭" style="width:200px" />
	      </div>
	  </div>
	</div>  

	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript" src="plugins/date.js" ></script>
	<script type="text/javascript" src="plugins/iscroll.js" ></script>
	<script src="plugins/jquery.scs.min.js"></script>
	<script src="plugins/CNAddrArr.min.js"></script>
	<script src="plugins/datepicker/bootstrap-datepicker.js"></script>

	<script>
	  $('.datepicker').datepicker({
	    autoclose: true
	  });

	  function back(){
	    location.href = history.go(-1);
	  }

	  $(".undertake").click(function(){
	  	var pid = $(".transfer_undertake").val();
	  	if (pid) {
		    $(".search_box").show();
		    $(".btn-submit").removeClass('btn-user');
		    $(".btn-submit").addClass('btn-undertake');
		    var url = "<?= Url::toRoute('order/getdepart')?>";
		    var _csrf ="<?= Yii::$app->request->getCsrfToken()?>";
		    $.ajax({
		      url:url,
		      type:'GET',
		      dataType:'json',
		      data: {
		        pid:pid,
		        _csrf:_csrf
		      },
		      success: function(json){
		      	if (json.flag) {
		      		$(".depart").html("<option value=''>"+json.depart+"</option>");
		      		var html = "";
		      		$.each(json.employee, function (n, value) {
	                  html += "<option value='"+value.id+"'>"+value.realname+"</option>";
	                });
	                $(".employees").html(html);
		      	}
		      },
		      error:function(){
		        alert('网络忙');
		      }
		    });
	  	}
	  });

	  $(".depart").change(function(){
	    var did = $(this).val();
	    var url = "<?= Url::toRoute('customer/getemployee')?>";
	    var _csrf ="<?= Yii::$app->request->getCsrfToken()?>";
	    $.ajax({
	      url:url,
	      type:'GET',
	      dataType:'json',
	      data: {
	        did:did,
	        _csrf:_csrf
	      },
	      success: function(json){
	        var html = "";
	        if (json.flag) {
	                $.each(json.data, function (n, value) {
	                  html += "<option value='"+n+"'>"+value+"</option>";
	                });
	        } else {
	          html = "<option value=''>部门暂无员工</option>";
	        }
	        $(".employees").html(html);
	      },
	      error:function(){
	        alert('网络忙');
	      }
	    }); 
	  });

	  $(".search_box .zhezhao").click(function(){
	    $(this).parent(".search_box").hide();
	  });

	  $(".btn-close").click(function(){
	    $(".search_box").hide();
	  });

	  $(".box-footer").on('click', '.btn-undertake', function(){
	  	var employee = $(".employees").val();
	  	var name = $('.employees option:selected').text();
	  	if (employee) {
	  		$(".search_box").hide();
	  		$(".undertake").val(name);
	  		$(".underval").val(employee);
	  	} else {
	  		alert('请选择承接人');
	  	}
	  });
	</script> 

	<script>
		$(".username").click(function(){
			$(".btn-submit").removeClass('btn-undertake');
			$(".btn-submit").addClass('btn-user');
			$(".search_box").show();
			var depart = '<?= $depart?>';
			var html = "";
            $.each(JSON.parse(depart), function (n, value) {
              html += "<option value='"+n+"'>"+value+"</option>";
            });
            $(".depart").html(html);
		});

	  $(".box-footer").on('click', '.btn-user', function(){
	  	var employee = $(".employees").val();
	  	var name = $('.employees option:selected').text();
	  	if (employee) {
	  		$(".search_box").hide();
	  		$(".username").val(name);
	  		$(".userval").val(employee);
	  	} else {
	  		alert('请选择对接人');
	  	}
	  });

	</script> 

	<script>
		$(".customer").change(function(){
			var cid = $(this).val();
	    	var url = "<?= Url::toRoute('order/getcustomer')?>";
		    var _csrf ="<?= Yii::$app->request->getCsrfToken()?>";
		    $.ajax({
		      url:url,
		      type:'GET',
		      dataType:'json',
		      data: {
		        cid:cid,
		        _csrf:_csrf
		      },
		      success: function(json){
		      	if (json.flag) {
		      		$(".customer-sex").val(json.sex);
		      		$(".customer-birthday").val(json.birthday);
		      	} else {
		      		$(".customer-sex").val('');
		      		$(".customer-birthday").val('');
		      	}
		      },
		      error:function(){
		        alert('网络忙');
		      }
		    });
		});
	</script>
	
	<script>
		$(".process").change(function(){
			var pcode = $(this).val();
	    	var url = "<?= Url::toRoute('order/getprocess')?>";
		    var _csrf ="<?= Yii::$app->request->getCsrfToken()?>";
		    $.ajax({
		      url:url,
		      type:'GET',
		      dataType:'json',
		      data: {
		        pcode:pcode,
		        _csrf:_csrf
		      },
		      success: function(json){
		      	if (json.flag) {
					var html = "";
		            $.each(json.data, function (n, value) {
		              html += "<option value='"+n+"'>"+value.tran_depart +'-'+value.undertake_depart+"</option>";
		            });
		            $(".transfer_undertake").html(html);
		      	}
		      },
		      error:function(){
		        alert('网络忙');
		      }
		    });

		});

		$(".choise_product").change(function(){
			var pid = $(this).val();
	    	var url = "<?= Url::toRoute('order/getprice')?>";
		    var _csrf ="<?= Yii::$app->request->getCsrfToken()?>";
		    if (pid) {
			    $.ajax({
			      url:url,
			      type:'GET',
			      dataType:'json',
			      data: {
			        pid:pid,
			        _csrf:_csrf
			      },
			      success: function(json){
			      	if (json.flag) {
			      		$(".product_price").val(json.price);
			      	}
			      },
			      error:function(){
			        alert('网络忙');
			      }
			    });
			} else {
				$(".product_price").val('');
			}

		});
	</script>